<template>
  <div class="header">
    <div class="header-back" v-show="!showHeader" @click="back">
      <span class="iconfont header-icon-back">&#xe658;</span>
    </div>
    <div class="header-fixed" :style="headerStyle" v-show="showHeader">
      <router-link to="/" tag="div" class="iconfont header-fixed-icon"
        >&#xe658;</router-link
      >
      大连圣亚海洋世界
    </div>
  </div>
</template>

<script>
import { onMounted, onUnmounted, reactive, ref } from "vue";
import { useRouter } from "vue-router";
export default {
  name: "DetailHeader",
  setup() {
    const showHeader = ref(false);
    const headerStyle = reactive({
      opacity: 0,
    });
    function handlePageScroll() {
      const distance =
        document.documentElement.scrollTop ||
        document.body.scrollTop ||
        window.pageYOffset;
      if (distance > 60) {
        let opa = distance / 140;
        opa = opa > 1 ? 1 : opa;
        headerStyle.opacity = opa;
        showHeader.value = true;
      } else {
        showHeader.value = false;
      }
    }
    const router = useRouter();
    function back() {
      router.push("/");
    }
    onMounted(() => {
      window.addEventListener("scroll", handlePageScroll);
    });
    onUnmounted(() => {
      window.removeEventListener("scroll", handlePageScroll);
    });

    return { showHeader, headerStyle,back };
  },
};
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';

.header-back {
  width: 0.8rem;
  height: 0.8rem;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 50%;
  position: absolute;
  top: 0.1rem;
  left: 0.2rem;
  text-align: center;

  .header-icon-back {
    font-size: 0.6rem;
    color: #fff;
    line-height: 0.8rem;
  }
}

.header-fixed {
  z-index: 2;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  height: $headerHeight;
  line-height: $headerHeight;
  color: #fff;
  background: $bgColor;
  text-align: center;
  font-size: 0.32rem;

  .header-fixed-icon {
    position: absolute;
    left: 0.14rem;
    color: #fff;
    width: 0.64rem;
    font-size: 0.5rem;
  }
}
</style>